#!/bin/bash

# 其他备份服务器ip及其地址
REMOTE_SERVER="172.16.0.6"
REMOTE_USER="root"
# 备份文件的保存路径
BACKUP_DIR="/data/backup/redis/$(date +%Y%m%d)"
# 创建备份文件保存目录
mkdir -p "${BACKUP_DIR}/"
LOG_FILE=${BACKUP_DIR}/info.log


start_time=$(date +"%Y-%m-%d %H:%M:%S.%3N")
start_seconds=$(date -d "$start_time" +%s%3N)
echo "$start_time 全量redis备份开始" >> ${LOG_FILE}


BACKUP_FILE="${BACKUP_DIR}/backup_redis_$(date +%Y%m%d%H%M%S).zip"
sudo zip -r ${BACKUP_FILE} /data/data/docker_volumn/redis 
if [ $? -eq 0 ]; then
    echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") 全量redis备份成功：${BACKUP_FILE}" >> ${LOG_FILE}
else
    echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") 全量redis备份失败：${BACKUP_FILE}" >> ${LOG_FILE}
fi



end_time=$(date +"%Y-%m-%d %H:%M:%S.%3N")
end_seconds=$(date -d "$end_time" +%s%3N)
duration=$((end_seconds - start_seconds))
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") 用时：$duration 毫秒" >>  ${LOG_FILE}


ssh ${REMOTE_USER}@${REMOTE_SERVER} "mkdir -p ${BACKUP_DIR}"
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") 远程[${REMOTE_SERVER}]创建文件夹[${BACKUP_DIR}]" >>  ${LOG_FILE}
scp ${BACKUP_FILE} ${BACKUP_USER}@${REMOTE_SERVER}:${BACKUP_DIR}
echo "$(date +"%Y-%m-%d %H:%M:%S.%3N") 发送文件[${BACKUP_FILE}]到远程[${REMOTE_SERVER}]服务器" >>  ${LOG_FILE}



